package model.input;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import view.input.InputWords;


public abstract class Input {

	
	public static String read() {
		String inputLine = null;
	
		try {
			InputStreamReader converter = new InputStreamReader(System.in);
			BufferedReader in = new BufferedReader(converter);
			inputLine=in.readLine();
			}
				catch (IOException ex){
					return null;
			}
				return inputLine.toUpperCase();
		}
	
	
	public InputWords getWords(String input){
		InputWords convWords= new InputWords();
		
		//String input=read();
		
		int spaceIndex1=-1;
		int spaceIndex2=-1;
		int spaceIndex3=-1;
		int numWords=1;
		
		convWords.setWord1(input);
		
		spaceIndex1=input.indexOf(" ");
		
		if (spaceIndex1>=0){
			convWords.setWord1(input.substring(0, spaceIndex1));
			spaceIndex2=input.indexOf(" ",spaceIndex1+1);
			if (spaceIndex2>=0){
				convWords.setWord2(input.substring(spaceIndex1+1, spaceIndex2));
				spaceIndex3=input.indexOf(" ",spaceIndex2+1);
				
				if (spaceIndex3>=0){
					if (input.indexOf(" ",spaceIndex3+1)>=0){
						System.out.println("He interrupts you, \"I'm not here to listen to you blather boy,\nget to the point. Summarise in four words, or less\"");
						numWords=5;
						convWords.setWord1("");
						convWords.setWord2("");
						convWords.setWord3("");
						convWords.setWord4("");
					}
					else{numWords=4;
						convWords.setWord4(input.substring(spaceIndex3+1, input.length()));
					}
				}
				else{numWords=3;
					convWords.setWord3(input.substring(spaceIndex2+1, input.length()));
				}
			}
			else{numWords=2;
				convWords.setWord2(input.substring(spaceIndex1+1, input.length()));
				}
		}
		else{numWords=1;
			convWords.setWord1(input.substring(0, input.length()));
			}

	
		return convWords;
	}
	
	// Have a look at this. I'm not sure if it has the same
	// effect as the above function but it seems to. And it's short.
	// It also occurs to me that this should be the ConversationInput
	// version of the function. It throws away command words like 'up'.
	public InputWords paddyGetWords(String input)
	{
		InputWords convWords= new InputWords();
		
		//String input=read();
		//System.out.println(input);
		String filtered = this.filterString(input);
		//System.out.println(filtered);
		
		input=input.toUpperCase();
		
		String[] words = filtered.split(" ");
		if( words.length > 4 )
		{
			System.out.println("Too many words mate. Try again (twat).");
			return convWords;			
		}
		
		for( String s : words )
		{
			convWords.add(s);
		}
		return convWords;
		
	}
	
	// This filter can filter any words you like
	public String filterString(String in)
	{
		String out = "";
		String[] prepositions = { "aboard","about","above","across","after","against","along",
	    "alongside","amid","amidst","among"," amongst","around","as","aside","astride","at",
	    "athwart","atop","barring","before","behind","below","beneath","beside","besides",
	    "between","beyond","but","by","can","circa","concerning","despite","down","during","except",
	    "failing","following","for","from","given","in","inside","into","like","mid","minus","near",
	    "next","notwithstanding","of","off","on","onto","opposite","out","outside","over","pace",
	    "past","per","plus","regarding","round","save","since","than","through","throughout","till",
	    "times","to","toward","towards","under","underneath","unlike","until","up","upon","versus",
	    "via","with","within","without","worth"};		
		
		
		// Then prepositions
		for( String s : in.split(" ") )
		{
			// This regexp removes all punctuation 
			s = s.replaceAll("[\\W]","");
			if( match(s,prepositions) )
			{
				// Don't need to say this
				System.out.println("Threw away '"+s+"'");
			}
			else
			{
				out += s + " ";
			}
		}		
		return out.trim();
	}
	
	// This match function matchs a single string to an array of strings
	public boolean match(String s, String[] array)
	{
		for(String m : array)
		{
			if(m.compareToIgnoreCase(s)==0) return true;
		}
		return false;
	}
}
